layui.use(['form', 'jquery', 'layer', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var upload = layui.upload;
	var post_data = [];
	var out_data = [];
	$('.config_excel').on('click', function() {
		if (post_data.length == 0) {
			layer.msg('没有可处理的订单信息');
			return false;
		}
		layer.confirm('确认处理订单吗？',{icon:3,title:'温馨提示：'},function(index){
			layer.close(index);
			send_info(save_state_uri, {
				info: post_data
			}, function(data) {
				layer.confirm('信息处理成功',{icon:3,title:'温馨提示'},function(index){
					parent.layer.closeAll();
					parent.layui.table.reload('invoice_list');
				})
			},function(){
				layer.close(index);
			})
		})
	})
	$('.out_excel').on('click', function() {
		if (out_data.length == 0) {
			layer.msg('没有可导出的订单信息');
			return false;
		}
		layer.load(2);
		send_info(out_export_url, {param:out_data}, function(data) {
			layer.closeAll();
			layer.open({
				type: 2,
				title: '下载excel',
				content: data.data,
				time: 500
			});
		})
	})
	var upload_invoice = upload.render({
		elem: '.up_excel',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(excel_uri + '?file=' + res.data.save_path, {}, function(data) {
				var html = ''
				data.data.ok.forEach(function(e, i) {
					let bg = '';
					if (e.state == 2) {
						bg = ' style="color:#009300"';
					} else if (e.state == 4) {
						bg = ' style="color:#1E9FFF"';
					} else if (e.state == 3) {
						bg = ' style="color:#FFB800"';
					}
					html = html + '<tr '+bg+'>';
					html = html + "<td>" + e.date + "</td><td>" + e.title + "</td><td>" + e.intro +"</td><td>" + e.money + "</td><td>" + e.order_no + "</td><td>" + e.state_text + "</td>";
					let temp = {
						order_no: e.order_no,
						state: e.state
					}
					post_data.push(temp);
					html = html + '</tr>';
				})
				data.data.out.forEach(function(e, i) {
					let bg = ' style="color:#FF5722"';
					html = html + '<tr '+bg+'>';
					html = html + "<td>" + e.date + "</td><td>" + e.title + "</td><td>" + e.intro +"</td><td>" + e.money + "</td><td>" + e.order_no + "</td><td>" + e.state_text + "</td>";
					html = html + '</tr>';
				})
				out_data = data.data.out;
				//post_data = data.data;
				console.log(post_data);
				$('#daoru').html(html);
				layer.closeAll();
			})

		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
})
